<!DOCTYPE HTML>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="gray-bg">
    
     <div class="container-div">
		<div class="row">
			<div class="col-sm-12 search-collapse">
				<form id="formId">
					<input id="treeId" name="relModuleId" type="hidden" />

					<div class="select-list">
						<ul>
							<li>
								用例名称：<input type="text" name="caseName"/>
							</li>


							<li>
								脚本名称：<input type="text" name="scriptName"/>
							</li>

							<li>
								项目:
								<select id="projectId" name="relProjectId" th:with="type=${projects}"
										onchange="initializeModuleTree()">
									<option value="">所有项目</option>
									<option th:each="projects : ${type}" th:text="${projects.projectName}"
											th:value="${projects.projectId}"
											th:selected="${projects.projectId == defaultProjectId}"></option>
								</select>
							</li>


							<li>
								模块：
								<input type="text" onclick="selectModuleTree()" id="treeName" readonly="true" value=""/>
							</li>


							<li>
								测试场景:
								<select id="scenario" name="scenario">
									<option value="" >全部</option>
									<option value="基准测试">基准测试</option>
									<option value="负载测试">负载测试</option>
									<option value="压力测试">压力测试</option>
									<option value="容量测试">容量测试</option>
								</select>
							</li>

							<li>
								<a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
								<a class="btn btn-warning btn-rounded btn-sm" onclick="reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
							</li>
						</ul>
					</div>
				</form>
			</div>

	        <div class="btn-group-sm hidden-xs" id="toolbar" role="group">
				<a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="jmeter:jmeterTestCase:add">
					<i class="fa fa-plus"></i> 添加
				</a>
				<a class="btn btn-primary btn-edit disabled" onclick="$.operate.edit()" shiro:hasPermission="jmeter:jmeterTestCase:edit">
					<i class="fa fa-edit"></i> 修改
				</a>
				<a class="btn btn-danger btn-del btn-del disabled" onclick="$.operate.removeAll()" shiro:hasPermission="jmeter:jmeterTestCase:remove">
					<i class="fa fa-remove"></i> 删除
				</a>
				<a class="btn btn-info btn-edit disabled" onclick="copyCase()">
					<i class="fa fa-clipboard"></i> 复制用例
				</a>
				<a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="jmeter:jmeterTestCase:export">
						<i class="fa fa-download"></i> 导出
				 </a>
			</div>
			<div class="col-sm-12 select-table table-striped">
				<table id="bootstrap-table" data-mobile-responsive="true"></table>
			</div>
		</div>
	</div>
    <div th:include="include :: footer"></div>
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('jmeter:jmeterTestCase:edit')}]];
        var removeFlag = [[${@permission.hasPermi('jmeter:jmeterTestCase:remove')}]];
        var prefix = ctx + "jmeter/jmeterTestCase";
		var alertConfigPrefix = ctx + "jmeter/jmeterAlertConfig";
		var modulePrefix = ctx + "testmanagmt/projectCaseModule";


		$(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
				exportUrl: prefix + "/export",
				copyUrl:  prefix + "/copy/{id}",
                modalName: "jmeter测试用例",
				search: false,
		        showExport: true,
                columns: [{
		            checkbox: true
		        },
				{
					field : 'id',
					title : '',
					visible: false
				},

				{
					field : 'relProjectName',
					title : '项目名称',
					sortable: true
				},
				{
					field: 'relModuleName',
					title: '模块名称',
					sortable: true
				},
				{
					field: 'interfaceName',
					title: '接口名称',
					sortable: true
				},
				{
					field: 'testPurpose',
					title: '测试目的',
					sortable: false
				},
				{
					field : 'caseName',
					title : '用例名称',
					sortable: true
				},

				{
					field : 'scriptName',
					title : '脚本名称',
					sortable: true
				},
				{
					field : 'caseNames',
					title : '节点分配',
					formatter: function(value, row, index) {
						var actions = [];
						actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="showCaseNode(\'' + row.id + '\')"><i class="fa fa-edit"></i>节点分配</a> ');
						return actions.join('');
					}
				},
				{
					title : '查看压力预估图',
					formatter: function(value, row, index) {
						var actions = [];
						actions.push('<a class="btn btn-navbar btn-xs ' + editFlag + '" href="#" onclick="showPressureGraph(\'' + row.id + '\')"><i class="fa fa-bar-chart"></i>查看压力预估图</a> ');
						return actions.join('');
					}
				},

				{
					title : '告警配置',
					formatter: function(value, row, index) {
						var actions = [];
						actions.push('<a class="btn btn-navbar btn-xs ' + editFlag + '" href="#" onclick="showAlertConfig(\'' + row.id + '\')"><i class="fa fa-bell"></i>告警配置</a> ');
						return actions.join('');
					}
				},

				{
					field: 'scenario',
					title: '测试场景',
					sortable: false
				},
				{
					field : '下载脚本',
					title : '下载脚本',
					formatter: function(value, row) {
						var actions = [];
						actions.push('<a class="btn btn-navbar btn-xs ' + editFlag + '" href="#" onclick="downloadJmx(\'' + row.id + '\')"><i class="fa fa-cloud-download"></i>下载</a> ');
						return actions.join('');
					}
				},
				{
					field : 'addTime',
					title : '创建时间',
					formatter: function (value,
										 row, index) {
						return $.common.formatTime(value);
					}
				},
				{
					field : 'updateTime',
					title : '修改时间',
					formatter: function (value,
										 row, index) {
						return $.common.formatTime(value);
					}
				},
		        {
		            title: '操作',
		            align: 'center',
		            formatter: function(value, row, index) {
		            	var actions = [];
		            	actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="#" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
						return actions.join('');
		            }
		        }]
            };
            $.table.init(options);
        });


		// 复制信息
		function copyCase(id) {
			if ($.common.isNotEmpty(id)) {
				url = $.table._option.copyUrl.replace("{id}", id);
			} else {
				var id = $.common.isEmpty($.table._option.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns($.table._option.uniqueId);
				if (id.length == 0) {
					$.modal.alertWarning("请至少选择一条记录");
					return;
				}
				url = $.table._option.copyUrl.replace("{id}", id);

				$.ajax({
					url: url,
					type: 'GET',
					cache: false,
					processData: false,
					contentType: false,
					success: function (result) {
						if (result.code == web_status.SUCCESS) {
							$.modal.msgSuccess("复制成功!");
							$.table.refresh();
						} else {
							$.modal.msgError(result.msg);
						}
					}
				});
			}
		}



		function showCaseNode(caseId) {
			var url = prefix + "/selectJmeterNode?caseId="+caseId;
			openFull(caseId,"", url);
		}


		function showPressureGraph(caseId) {
			let url = prefix + "/showPressureGraph?caseId="+caseId;
			//如果是移动端，就使用自适应大小弹窗
			if (navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)) {
				width = 'auto';
				height = 'auto';
			}
			let  index = layer.open({
				type: 2,
				area: ['auto', 'auto'],
				fix: false,
				//不固定
				// maxmin: true,
				shade: 0.3,
				anim: -1,
				title: '查看压力预估图',
				content: url,
				btn: ['关闭'],
				closeBtn: 0
			});
			layer.full(index);
		}


		function downloadJmx(caseId) {
			window.location=prefix+"/donwloadJmx?caseId="+caseId;
		}


		function showAlertConfig(caseId) {
			createMenuItem(alertConfigPrefix + "?caseId=" + caseId,'告警配置');
		}


		function openFull(caseId,title, url, width, height) {
			//如果是移动端，就使用自适应大小弹窗
			if (navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)) {
				width = 'auto';
				height = 'auto';
			}
			var index = layer.open({
				type: 2,
				area: [width + 'px', height + 'px'],
				fix: false,
				//不固定
				maxmin: true,
				shade: 0.3,
				title: title,
				content: url,
				btn: ["保存选择节点", '关闭'],
				// 弹层外区域关闭
				shadeClose: true,
				yes: function(index, layero) {
					var iframeWin = layero.find('iframe')[0];
					iframeWin.contentWindow.submitHandler(caseId,index);
					return true;
				},
				cancel: function(index) {
					return true;
				}
			});
			layer.full(index);
		}



		function doSubmit(index, layero) {
			var body = layer.getChildFrame('body', index);
			$("#treeId").val(body.find('#treeId').val());
			$("#treeName").val(body.find('#treeName').val());
			layer.close(index);
		}


		/*查询条件选择用例模块*/
		function selectModuleTree() {
			var projectId = $("#projectId  option:selected").val();
			if (projectId == "") {
				$.modal.alertError("请先选择项目");
			} else {
				var options = {
					title: '模块选择',
					width: "380",
					url: modulePrefix + "/selectProjectCaseModuleTree/" + $("#treeId").val(),
					callBack: doSubmit
				};
				$.modal.openOptions(options);
			}
		}

		/*选择项目初始化模块树*/
		function initializeModuleTree() {
			var projectId = $("#projectId  option:selected").val();
			if (projectId == "") {
				$("#treeId").val("");
				$("#treeName").val("");
			} else {
				$.ajax({
					cache: true,
					type: "GET",
					url: modulePrefix + "/getModuleByProjectId/" + projectId,
					data: {},
					async: false,
					error: function (request) {
						$.modal.alertError("系统错误");
					},
					success: function (data) {
						$("#treeId").val(data.moduleId);
						$("#treeName").val(data.moduleName);
					}
				});
			}
		}


		 function reset() {
			$("#formId")[0].reset();
			 $("#treeId").val("");

		 }


	</script>
</body>
</html>